package org.oep;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class ValueScroller extends CustomItem {
	private int value, minimum, maximum, initial;
	String units;
	
	ValueScroller(String label, String units, int minimum, int maximum, int initial) {
		super(label);
		this.units = units;
		this.minimum = minimum;
		this.maximum = maximum;
		this.value = initial;
		this.initial = initial;
	}

	protected int getMinContentHeight() {
		return 10;
	}

	protected int getMinContentWidth() {
		return 40;
	}

	protected int getPrefContentHeight(int w) {
		return 25;
	}

	protected int getPrefContentWidth(int h) {
		return 75;
	}

	protected void paint(Graphics g, int w, int h) {
		g.fillTriangle(0, h / 2, 5, h / 4, 5, (3 * h) / 4);
		g.drawString(value + units, w / 2, h / 2 + g.getFont().getHeight() / 2, Graphics.HCENTER | Graphics.BOTTOM);
		g.fillTriangle(w, h / 2, w - 5, h / 4, w - 5, (3 * h) / 4);
	}
	
	public void keyPressed(int keyCode) {
		switch(keyCode) {
		case Canvas.RIGHT:
			if(value < maximum)
				value++;
			repaint();
			break;
		case Canvas.LEFT:
			if(value > minimum)
				value--;
			repaint();
			break;
		}
	}
	
	public void keyRepeated(int keyCode) {
		keyPressed(keyCode);
	}
	
	public int getValue() { return value; }
	public void setValue(int i) { if(i <= maximum && i >= minimum) value = i; }
	
	public int getDefault() { return initial; }
	public void setDefault(int i) { if(i <= maximum && i >= minimum) initial = i; }

}
